//
//  XFOauthorViewController.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/11.
//  Copyright © 2015年 BlackTail. All rights reserved.
// App Key：2183042437
// App Secret：3ec2a75c3118a63c78547813e349de08
// http://www.baidu.com
#import "XFOauthorViewController.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "XFAccount.h"
#import "XFUserAccountModel.h"
@interface XFOauthorViewController () <UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@end

@implementation XFOauthorViewController

- (void)loadView {
  UIWebView *webView = [[UIWebView alloc] init];
  self.view = webView;
  self.webView = webView;
  self.webView.delegate = self;
}

- (void)viewDidAppear:(BOOL)animated {

  [SVProgressHUD show];
  [super viewDidAppear:animated];
}

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setupwebView];

  self.navigationItem.leftBarButtonItem =
      [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                       style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(backDidClick)];
  self.navigationItem.rightBarButtonItem =
      [[UIBarButtonItem alloc] initWithTitle:@"自动补全"
                                       style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(fullDidClick)];
}

- (void)backDidClick {
  [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)fullDidClick {
    // 835889160@qq.com   womenlia 15515064587  gxf5211314  469770143@qq.com 17v77.com
  NSString *js = @"document.getElementById('userId').value = "
      @"'15515064587',document.getElementById('passwd').value = "
      @"'gxf5211314';";
  [self.webView stringByEvaluatingJavaScriptFromString:js];
}

- (void)setupwebView {
  NSString *urlString =
      [NSString stringWithFormat:@"https://api.weibo.com/oauth2/"
                                 @"authorize?client_id=%@&redirect_uri=%@",
                                 kClient_id, kRedirect_uri];
  NSURLRequest *request =
      [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
  [self.webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
  [SVProgressHUD show];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  [SVProgressHUD dismiss];
}

- (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request
                navigationType:(UIWebViewNavigationType)navigationType {
  //  NSLog(@"%@", request);
  NSString *urlString = request.URL.absoluteString;
  //判断是否存在 url
  if (urlString) {

    if ([urlString hasPrefix:@"https://api.weibo"]) {
      return YES;
    }

    if ([urlString hasPrefix:@"http://www.baidu.com"]) {
      NSString *query = request.URL.query;
      //判断是否 query为空
      if (query) {
        NSString *code = [query stringByReplacingOccurrencesOfString:@"code="
                                                          withString:@""];
        XFUserAccountModel *userAccout = [[XFUserAccountModel alloc] init];

        [userAccout
            getAccess_tokenWithCode:code
                          andfinish:^(NSError *error, NSArray *array) {
                            if (!error) {
                              [self dismissViewControllerAnimated:
                                        NO completion:^{

                                [[NSNotificationCenter defaultCenter]
                                    postNotificationName:@"selectViewcontroller"
                                                  object:@"welcome"];

                              }];
                            } else {
                                [SVProgressHUD showErrorWithStatus:kErrorTip];
                              NSLog(@"%@", error);
                            }

                          }];
      }
    }
  }
  return NO;
}

@end
